package pl.edu.pw.MidEmu.UI.Components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.util.Hashtable;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import pl.edu.pw.MidEmu.Base.Components.BaseFrame;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.Models.ComponentTypeModel;
import pl.edu.pw.MidEmu.UI.Controllers.MainFrameController;
import pl.edu.pw.MidEmu.UI.Views.MainFrameView;

/**
 * Main application frame with GUI for MidEmu workspace
 * @author Mateusz Starodab
 *
 */
public class MainFrame extends BaseFrame<JFrame, MainFrameController> implements MainFrameView
{
	private JMenuBar mbMainMenu;
	private JMenu mFile;
	private JMenu mHelp;
	private JMenuItem miAbout;

	private JMenuItem miAuthor;
	private JMenuItem miExit;
	private JMenuItem miNetwork;
	private JMenuItem miConnectivity;
	private JMenu mSettings;
	private JPanel pnlCenter;
	private JPanel pnlMain;
	private JScrollPane pnlScroll;
	private JPanel pnlWest;

	public MainFrame()
	{
		MainFrameController controller = new MainFrameController();
		controller.setView(this);
		setController(controller);
		JFrame frame = new JFrame();
		setComponent(frame);

		getComponent().setTitle(Consts.MainFrame_Title);
		getComponent().setSize(800, 600);
		getComponent().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getComponent().setLocationByPlatform(true);

		mbMainMenu = new JMenuBar();
		mFile = new JMenu(Consts.MainFrame_FileMenu);
		miExit = new JMenuItem(Consts.MainFrame_ExitMenu);
		miExit.setName(Consts.MainFrame_ExitMenu);
		miExit.addActionListener(getController());

		mFile.add(miExit);
		mbMainMenu.add(mFile);

		mSettings = new JMenu(Consts.MainFrame_SettingsMenu);

		miNetwork = new JMenuItem(Consts.MainFrame_NetworkMenu);
		miNetwork.setName(Consts.MainFrame_NetworkMenu);
		miNetwork.addActionListener(getController());

		miConnectivity = new JMenuItem(Consts.MainFrame_ConnectivityMenu);
		miConnectivity.setName(Consts.MainFrame_ConnectivityMenu);
		miConnectivity.addActionListener(getController());

		mSettings.add(miNetwork);
		mSettings.add(miConnectivity);

		mbMainMenu.add(mSettings);

		mHelp = new JMenu(Consts.MainFrame_HelpMenu);

		miAbout = new JMenuItem(Consts.MainFrame_AboutMenu);
		miAbout.setName(Consts.MainFrame_AboutMenu);
		miAbout.addActionListener(getController());
		mHelp.add(miAbout);
		mHelp.addSeparator();
		miAuthor = new JMenuItem(Consts.MainFrame_AuthorMenu);
		miAuthor.setName(Consts.MainFrame_AuthorMenu);
		miAuthor.addActionListener(getController());
		mHelp.add(miAuthor);

		mbMainMenu.add(mHelp);

		getComponent().setJMenuBar(mbMainMenu);

		pnlMain = new JPanel();
		pnlMain.setLayout(new BorderLayout());

		pnlWest = new JPanel();
		pnlWest.setLayout(new FlowLayout(FlowLayout.LEFT));

		Dimension maxDimmention = getController().getButtonMaxDimmension();

		for (int i = 0; i < getController().getComponentTypes().size(); i++)
		{
			ComponentTypeModel model = getController().getComponentType(i);
			Icon icon = model.getIcon();
			JButton button = new JButton(icon);
			button.setName("component_" + i);
			button.setToolTipText(model.getComponentDescription());
			button.addActionListener(getController());
			button.setPreferredSize(maxDimmention);
			pnlWest.add(button);
		}

		pnlWest.setPreferredSize(new Dimension((int) maxDimmention.getWidth() + 10, (int) (getController().getComponentTypes().size() * (maxDimmention.getHeight() + 5) + 5)));

		pnlScroll = new JScrollPane(pnlWest, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		pnlMain.add(pnlScroll, BorderLayout.WEST);

		pnlCenter = new JPanel();
		pnlCenter.setLayout(null);
		pnlMain.add(pnlCenter, BorderLayout.CENTER);

		getComponent().setContentPane(pnlMain);

		getComponent().addWindowListener(getController());
	}

	public MidasComponent createComponent(String index)
	{
		ComponentTypeModel model = getController().getComponentType(Integer.parseInt(index));
		int freeIndex = getController().getFirstFreeIndexForComponents(model.getComponentType());

		MidasComponent component = new MidasComponent(this, model, freeIndex);
		pnlCenter.add(component.getComponent());
		pnlCenter.repaint();

		component.init();

		return component;
	}

	@Override
	public void dispose()
	{
		getComponent().setVisible(false);
		getComponent().dispose();
	}

	public void remove(MidasComponent midasComponent)
	{
		getController().removeMidasComponent(midasComponent);
		pnlCenter.remove(midasComponent.getComponent());
		pnlCenter.repaint();
	}

	@Override
	public void showNetworkSettingsFrame(Point point)
	{
		NetworkSettingsFrame frame = new NetworkSettingsFrame(this);
		frame.getComponent().setLocation(point);
		frame.getComponent().setVisible(true);
	}

	@Override
	public void showNetworkConnectivityFrame(Point point)
	{
		NetworkConnectivityFrame frame = new NetworkConnectivityFrame(this);
		if (frame.canRun())
		{
			frame.getComponent().setLocation(point);
			frame.getComponent().setVisible(true);
		}
		else
		{
			frame.dispose();
		}
	}

	public Hashtable<String, String> getNodesIDsAndIPs()
	{
		return getController().getNodesIDsAndIPs();
	}

	public Hashtable<String, String> getNodesInterfacesAndIPS()
	{
		return getController().getNodesInterfacesAndIPS();
	}
}
